package com.ry.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.mapper.AuthorMapper;
import com.ry.novel.mapper.UserMapper;
import com.ry.novel.model.dto.AuthorInfo;
import com.ry.novel.model.dto.condition.AuthorQuery;
import com.ry.novel.model.entity.Author;
import com.ry.novel.model.entity.User;
import com.ry.novel.service.AuthorService;
import com.ry.novel.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ry
 * @since 2022-04-07 07:26
 **/
@Service
public class AuthorServiceImpl implements AuthorService {
    @Autowired
    private AuthorMapper authorMapper;


    @Autowired
    private UserMapper userMapper;


    @Override
    public Page<AuthorInfo> queryAuthor(AuthorQuery authorQuery) {
        Page<Author> page = new Page<>(authorQuery.getPage(),authorQuery.getSize());
        Page<AuthorInfo> infoPage = new Page<>(authorQuery.getPage(),authorQuery.getSize());
        QueryWrapper<Author> wrapper = new QueryWrapper<>();
        if (authorQuery.getPenName() != null){
            wrapper.like("pen_name",authorQuery.getPenName());
        }

        authorMapper.selectPage(page, wrapper);
        List<AuthorInfo> infos = page.getRecords().stream().map(entity -> {
            AuthorInfo authorInfo = new AuthorInfo();
            authorInfo.setId(entity.getId());
            authorInfo.setUserId(entity.getUserId());
            User user = userMapper.selectById(entity.getUserId());
            if (user != null){
                authorInfo.setUsername(user.getUsername());
            }
            authorInfo.setPhone(entity.getPhone());
            authorInfo.setPenName(entity.getPenName());
            authorInfo.setState(entity.getState());
            authorInfo.setCreateTime(entity.getCreateTime());
            return authorInfo;
        }).collect(Collectors.toList());

        infoPage.setTotal(page.getTotal());
        infoPage.setRecords(infos);


        return infoPage;
    }

    @Override
    public List<AuthorInfo> queryAll() {
        List<Author> authors = authorMapper.selectList(new QueryWrapper<>());
        List<AuthorInfo> infos = authors.stream().map(entity -> {
            AuthorInfo authorInfo = new AuthorInfo();
            authorInfo.setId(entity.getId());
            authorInfo.setUserId(entity.getUserId());
            authorInfo.setUsername(entity.getPenName());
            authorInfo.setPhone(entity.getPhone());
            authorInfo.setPenName(entity.getPenName());
            authorInfo.setState(entity.getState());
            authorInfo.setCreateTime(entity.getCreateTime());

            return authorInfo;
        }).collect(Collectors.toList());

        return infos;
    }

    @Override
    public void addAuthor(AuthorInfo info) {
        Author author = new Author();
        author.setUserId(info.getUserId());
        author.setPhone(info.getPhone());
        author.setPenName(info.getPenName());
        author.setState(info.getState());
        author.setCreateTime(LocalDateTime.now().toString());

        authorMapper.insert(author);
    }

    @Override
    public void deleteAuthor(Long id) {
        authorMapper.deleteById(id);
    }

    @Override
    public void updateAuthor(AuthorInfo info) {
        Author author = new Author();
        author.setId(info.getId());
        author.setPhone(info.getPhone());
        author.setPenName(info.getPenName());
        author.setState(info.getState());

        authorMapper.updateById(author);
    }
}
